package Sort;

import java.util.Arrays;

public class SelectP {
    public static void main(String[] args) {
        int[] arr = new int[]{8, 782, 5, 2, 7, 1, 8, 6, 8, 3};
        /*int[] select = se2(arr);*/
        int[] select2 = select(arr);

    }

    public static int[] select(int[] arr) {
        int head = 0;
        int a = 0;
        int index = 1;
        int flag = 0;
        int fow = arr.length - 1;

        while (true) {


            for (int i = 0; i < fow; i++) {
                flag = head;
                if (arr[head] > arr[index]) {
                    flag = index;
                }
                index++;
                if (flag != head) {
                    swap(arr, flag, head);
                }

            }

            head++;
            index = head + 1;
            fow--;
            System.out.println(Arrays.toString(arr));
            if (index > arr.length - 1) {
                break;
            }
        }
        return arr;

    }

    public static int[] se2(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int s = i;

            for (int j = s + 1; j < arr.length; j++) {
                if (arr[s] > arr[j]) {
                    s = j;
                }
            }
            if (s != i) {
                swap(arr, s, i);
            }
            System.out.println(Arrays.toString(arr));
        }
        return arr;
    }

    public static void swap(int[] arr, int i, int j) {
        int t = arr[i];//基本数据类型可以这样 ，但是引用类型就要clone
        arr[i] = arr[j];
        arr[j] = t;

    }
}
